﻿using AutoMapper;
using B.S.Filtration.Domain;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Write.Api.Application.Command.ProcessCommand;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Write.Api.Application.CommandHander.ProcessHandler
{
    public class UpdateProcedureCommandHandler : IRequestHandler<UpdateProcedureCommand, APIResult<object>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public UpdateProcedureCommandHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(UpdateProcedureCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.code = APIResultCode.success;
            result.msg = "修改成功";

            var model = db.Queryable<Procedure>().Where(p => p.ProcedureId == request.ProcedureId);
            if (model.Count() != 0)
            {
                var production = mapper.Map<Procedure>(request);
                await db.Updateable<Procedure>(production).ExecuteCommandAsync();
            }

            return await Task.FromResult(result);
        }
    }
}
